package com.start.boot.web;

import com.start.boot.common.MessageResult;
import com.start.boot.common.SystemConfiguration;
import com.start.boot.domain.Notice;
import com.start.boot.service.NoticeService;
import com.start.boot.service.SystemCoreConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 通知公告
 *
 * @caomin
 * @create 2017-12-21 10:44
 **/
@RequestMapping("/notice")
@Controller
public class NoticeController extends ArchivesSystemBaseController {


    @Autowired
    SystemCoreConfigService systemCoreConfigService;

    @Autowired
    NoticeService noticeService;


    @ResponseBody
    @PostMapping("/saveNotice")
    @ApiOperation("添加消息")
    public MessageResult saveFile(@RequestBody Notice notice) throws IOException {
        notice.setFbrdwbm(getCurrentDwbm());
        notice.setFbrdwmc(getCurrentDwmc());
        notice.setFbrgh(getCurrentGh());
        notice.setFbrxm(getCurrentMC());
        Notice save = noticeService.save(notice);
        return  new MessageResult("保存成功",200,save);
    }



    @ResponseBody
    @PostMapping("/saveNoticeFile")
    @ApiOperation("获取单个通知")
    public MessageResult getSingeNotice(HttpServletRequest request, MultipartFile file) throws IOException {
        String wzbsPath = SystemConfiguration.wzbsPath;
        String notice_file_path = systemCoreConfigService.getSystemConfigValue("notice_file_path");
        String path=wzbsPath+notice_file_path;
        File file1 = new File(path + "//"+ UUID.randomUUID().toString()+file.getOriginalFilename());
        String bh = request.getParameter("bh");
        Notice notice = noticeService.getNoticeByBh(bh);
        notice.setFj(notice_file_path+"/"+file1.getName());
        if (!file1.getParentFile().exists()){
            file1.getParentFile().mkdirs();
        }
        file.transferTo(file1);
        notice.setZlmc(file.getOriginalFilename());
        noticeService.saveNotice(notice);
        return  new MessageResult("查询成功",200);
    }



    @ResponseBody
    @GetMapping("/getNotice")
    @ApiOperation("获取单个通知")
   public MessageResult getSingeNotice(String bh){
       Notice noticeByBh = noticeService.getNoticeByBh(bh);
       return  new MessageResult("查询成功",200,noticeByBh);
   }



}
